﻿using System;
using System.ComponentModel;
using System.Linq;

namespace N2.ComponentModel
{
	public class ChildrenEditor : ContentCollectionEditor
	{
		#region Constructors

		public ChildrenEditor(Type type) : base(type) { }

		#endregion Constructors

		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			return base.EditValue(context, provider, value);
		}

		protected override Type CreateCollectionItemType()
		{
			return typeof(ContentItem);
		}

		protected override object CreateInstance(Type itemType)
		{
			return N2.Context.Definitions.CreateInstance(
				itemType,
				(ContentItem)this.Context.Instance);
		}

		protected override Type[] CreateNewItemTypes()
		{
			var _definition = N2.Context.Definitions.GetDefinition(this.Context.Instance.GetType());
			return
				N2.Context.Definitions.GetAllowedChildren(
						_definition,
						null,
						null)
					.Select(_def => _def.ItemType)
					.ToArray();
		}
	}
}
